-- 流控中心
local log       = require("modules.commhm.log");
local ngx = ngx
local pairs = pairs

local mis_config = nil
local mis_log = nil
local mis_bridge = nil
local mis_data = nil
local mis_act = nil
local mis_cmd = nil
local t_retCode = nil

--------------------------------------------------------------
local mis_center = {
	s_name = string.match(debug.getinfo(1).short_src, '.*/(.*).lua');	-- 模块名称
}
local this = mis_center
local t_m_acts = {}
local t_m_work = {}
local t_m_cmds = {}
local t_m_ctrlGroup

--------------------------------------------------------------
-- 说明：模块包含
-- 参数：模块表
this.modules_include = function(t_moddules)
	mis_config = t_moddules.mis_config
	mis_log = t_moddules.mis_log
	mis_bridge = t_moddules.mis_bridge
	mis_data = t_moddules.mis_data
	mis_act = t_moddules.mis_act
	mis_cmd = t_moddules.mis_cmd
	t_retCode = mis_config.t_retCode
	t_m_ctrlGroup = { 			-- 按元素顺序初始化
		t_moddules.mis_qps;
		t_moddules.mis_saveAss;
		t_moddules.mis_user;
		t_moddules.mis_data;
		t_moddules.mis_mysql;
		t_moddules.mis_weekendMail;
		t_moddules.mis_annualVote;
		t_moddules.mis_allowIp;
		t_moddules.mis_weekendPush;
		t_moddules.mis_awakening;
		this;
	}
end

--------------------------------------------------------------
-- 说明：入口函数, 启动nginx之后只执行一次
-- 返回：b 		执行结果
this.enter_once = function()
	local n_code
	local s_desc
	for _, t_v in pairs(t_m_ctrlGroup) do
		if t_v.init_once then
			n_code, s_desc = t_v.init_once()
			if n_code ~= t_retCode.n_OK then
				mis_bridge.send(n_code, s_desc)
				return false
			end
		end
	end
	return true
end

--------------------------------------------------------------
-- 说明：入口函数，每次load之后执行一次
-- 返回：b		执行结果
this.enter_load = function()
	local n_code
	local s_desc
	for _, t_v in pairs(t_m_ctrlGroup) do
		if t_v.init_load then
			n_code, s_desc = t_v.init_load()
			if n_code ~= t_retCode.n_OK then
				mis_bridge.send(n_code, s_desc)
				return false
			end
		end
	end
	return true
end

--------------------------------------------------------------
-- 说明：入口函数，每次请求都会执行一次
-- 返回：b		执行结果
this.enter_request = function()
	local n_code, s_desc
	for _, t_v in pairs(t_m_ctrlGroup) do
		if t_v.init_request then
			n_code, s_desc = t_v.init_request()
			if n_code ~= t_retCode.n_OK then
				mis_bridge.send(n_code, s_desc)
				return false
			end
		end
	end

	local t_params = ngx.ctx.t_params
	if t_params.act then
		if t_m_acts[t_params.act] then
			t_m_acts[t_params.act](t_params.act)
		else
			s_desc = 'unknown act = ' .. t_params.act
			mis_log.error(s_desc)
			mis_bridge.send(t_retCode.n_PARAM_ERR, s_desc)
			return false
		end
	elseif t_params.cmd then
		if t_m_cmds[t_params.cmd] then
			t_m_cmds[t_params.cmd](t_params.cmd)
		else
			s_desc = 'unknown cmd = ' .. t_params.cmd
			mis_log.error(s_desc)
			mis_bridge.send(t_retCode.n_PARAM_ERR, s_desc)
			return false
		end
	else
		s_desc = 'no oper'
		mis_log.error(s_desc)
		mis_bridge.send(t_retCode.n_PARAM_ERR, s_desc)
		return false
	end

	return true
end

--------------------------------------------------------------
-- 说明：析构
this.enter_finish = function()
	for _, t_ in pairs(t_m_ctrlGroup) do
		if t_.finish_request then
			t_.finish_request()
		end
	end
end

--------------------------------------------------------------
-- 说明：act命令函数模板
local function act_operation_base(s_act)
	local s_work = t_m_work[s_act]
	local t_conf = mis_config.t_acts[s_work][s_act]

	local n_code, ts_desc = mis_data.check_fix_params()
	if n_code ~= t_retCode.n_OK then
		mis_bridge.send(n_code, ts_desc)
		return
	end
	--n_code, ts_desc = mis_data.check_auth()
	n_code, ts_desc = mis_data.check_auth_by_ver()
	if n_code ~= t_retCode.n_OK then
		mis_bridge.send(n_code, ts_desc)
		return
	end
	n_code, ts_desc = mis_data.check_params(t_conf)
	if n_code ~= t_retCode.n_OK then
		mis_bridge.send(n_code, ts_desc)
		return
	end
	n_code, ts_desc = mis_data.type_correct(t_conf)
	if n_code ~= t_retCode.n_OK then
		mis_bridge.send(n_code, ts_desc)
		return
	end
	n_code, ts_desc = mis_data.length_limit(t_conf)
	if n_code ~= t_retCode.n_OK then
		mis_bridge.send(n_code, ts_desc)
		return
	end
	n_code, ts_desc = mis_data.check_request_valid()
	if n_code ~= t_retCode.n_OK then
		mis_bridge.send(n_code, ts_desc)
		return
	end
	mis_data.set_global_vars(t_conf)
	-- 业务前置处理
	if mis_act['pre_' .. s_work] then
		n_code, ts_desc = mis_act['pre_' .. s_work]()
		if n_code ~= t_retCode.n_OK then
			mis_bridge.send(n_code, ts_desc)
			return
		end
	end
	-- 业务处理
	n_code, ts_desc = mis_act[s_act]()
	-- 响应
	if ngx.ctx.t_params and ngx.ctx.t_params.responseType == "json" then
		mis_bridge.send(n_code, ts_desc, 1)
	else
		mis_bridge.send(n_code, ts_desc, t_conf.n_responseType)
	end
end

--------------------------------------------------------------
-- 说明：cmd命令函数模板
local function cmd_operation_base(s_act)
	local t_conf = mis_config.t_cmds[s_act]

	local n_code, ts_desc = mis_data.check_fix_params()
	if n_code ~= t_retCode.n_OK then
		mis_bridge.send(n_code, ts_desc)
		return
	end
	n_code, ts_desc = mis_data.check_auth_by_ver()
	if n_code ~= t_retCode.n_OK then
		mis_bridge.send(n_code, ts_desc)
		return
	end
	n_code, ts_desc = mis_data.check_params(t_conf)
	if n_code ~= t_retCode.n_OK then
		mis_bridge.send(n_code, ts_desc)
		return
	end
	n_code, ts_desc = mis_data.type_correct(t_conf)
	if n_code ~= t_retCode.n_OK then
		mis_bridge.send(n_code, ts_desc)
		return
	end
	mis_data.set_global_vars(t_conf)
	n_code, ts_desc = mis_cmd[s_act]()
	mis_bridge.send(n_code, ts_desc, t_conf.n_responseType)
end

--------------------------------------------------------------
-- 说明：load级初始化
-- 返回：n		状态码
this.init_load = function()
	for s_work, t_ in pairs(mis_config.t_acts) do
		for s_func, _ in pairs(t_) do
			t_m_acts[s_func] = act_operation_base
			t_m_work[s_func] = s_work
		end
	end
	for s_, _ in pairs(mis_config.t_cmds) do
		t_m_cmds[s_] = cmd_operation_base
	end
	return t_retCode.n_OK
end

return this